package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.abel533.echarts.Grid;
import com.github.abel533.echarts.Legend;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.Title;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Pie;
import com.monitor.dao.*;
import com.monitor.entity.ErpCarTireArchives;
import com.monitor.entity.ErpCustomer;
import com.monitor.entity.ErpCustomerCar;
import com.monitor.entity.ErpMateriel;
import com.monitor.entity.ErpTireArchives;
import com.monitor.entity.ErpTirePolice;
import com.monitor.entity.JfzRealTime;
import com.monitor.service.JfzRealTimeService;
import com.monitor.util.BlankUtil;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import com.monitor.util.enums.PositonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.github.abel533.echarts.code.LegendType.scroll;


@Service
@Transactional
public class JfzRealTimeServiceImpl  extends ServiceImpl<JfzRealTimeMapper, JfzRealTime> implements JfzRealTimeService {
	@Resource
	private JfzRealTimeMapper jfzRealTimeMapper;
	
	@Resource
	private JfzCarTypeMapper jfzCarTypeMapper;
	
	@Resource
	private ErpCustomerCarMapper erpCustomerCarMapper;
	
	@Resource
	private ErpTirePoliceMapper erpTirePoliceMapper;
	
	@Resource
	private ErpTpmsTireInformationMapper erpTpmsTireInformationMapper;
	
	@Resource
	private ErpCustomerMapper erpCustomerMapper;
	
	@Resource
	private ErpMaterielMapper erpMaterielMapper;
	
	@Resource
	private ErpTireArchivesMapper erpTireArchivesMapper;
	
	private String[] positionNum=new String[] {"01","13"};
	
	@Override
	public Map<String, Object> getList(int page, int limit) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> map=new HashMap<>();
			int index = (page - 1) * limit;
			List<Map<String, Object>> data=jfzRealTimeMapper.getList(index, limit);
			int total=jfzRealTimeMapper.getCount();
			map.put("data", data);
			map.put("pageIndex", page);
			int totalPages = (total - 1) / limit + 1;
			map.put("totalPages", totalPages);
			map.put("total", total);
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Map<String, Object> getListByCar(int page, int limit) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> map=new HashMap<>();
			limit=50;
			int index = (page - 1) * limit;
			List<Map<String, Object>> data=jfzRealTimeMapper.getCarList(index, limit);
			/*for(Map<String, Object> ma:data) {
				int carId=Integer.parseInt(ma.get("id").toString());
				Double speed=jfzRealTimeMapper.selectSpeedByCarId(carId);
				if(speed==null) {
					ma.put("speed", 0);
				}else {
					ma.put("speed", speed);
				}
				String time=jfzRealTimeMapper.selectTimeByCarId(carId);
				if(time!=null&&!time.equals("")) {
					ma.put("time", time);
				}else {
					ma.put("time", "");
				}
				JfzCarType ct=jfzCarTypeMapper.selectByCarId(carId);
				if(ct!=null) {
					ma.put("type", ct.getType());
				}else {
					ma.put("type", 0);
				}
				for(String po:positionNum) {
					Map<String, Object> positionMap=jfzRealTimeMapper.selectCarByIdAndPosition(carId, po);
					if(positionMap!=null) {
						if(positionMap.get("voltage")!=null) {
							ma.put("voltage"+po, positionMap.get("voltage"));
						}else {
							ma.put("voltage"+po, "0");
						}
						if(positionMap.get("temperature")!=null) {
							ma.put("temperature"+po, positionMap.get("temperature"));
						}else {
							ma.put("temperature"+po, "0");
						}
						if(positionMap.get("tire_pressure")!=null) {
							ma.put("tire_pressure"+po, positionMap.get("tire_pressure"));
						}else {
							ma.put("tire_pressure"+po, "0");
						}
					}else {
						ma.put("voltage"+po, "0");
						ma.put("temperature"+po, "0");
						ma.put("pressure"+po, "0");
					}
				}*/
				
			int total=jfzRealTimeMapper.getCount();
			map.put("data", data);
			map.put("pageIndex", page);
			int totalPages = (total - 1) / limit + 1;
			map.put("totalPages", totalPages);
			map.put("total", total);
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Map<String, Object> getListByCustomerId(Integer custId,String keyWord) {
		try {
			ErpCustomer cust=erpCustomerMapper.selectById(custId);
			List<ErpCustomer> list=erpCustomerMapper.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com", cust.getCusCode()));
			List<Integer> custIdList=new ArrayList<>();
			custIdList.add(custId);
			for(ErpCustomer customer:list) {
				custIdList.add(customer.getId());
			}
			String custIds=BlankUtil.listToString(custIdList);
			Map<String, Object> map=new HashMap<>();
			StringBuffer sb=new StringBuffer(" ");
			if(keyWord!=null&&!keyWord.equals("")) {
				sb.append("AND (car.car_plate LIKE '%"+keyWord+"%' OR car.car_number LIKE '%"+keyWord+"%' OR cu.cus_name LIKE '%"+keyWord+"%') ");
			}
			List<Map<String, Object>> data=jfzRealTimeMapper.getCarListById(custIds,sb.toString());
			/*for(Map<String, Object> ma:data) {
				int carId=Integer.parseInt(ma.get("id").toString());
				Double speed=jfzRealTimeMapper.selectSpeedByCarId(carId);
				if(speed==null) {
					ma.put("speed", 0);
				}else {
					ma.put("speed", speed);
				}
				String time=jfzRealTimeMapper.selectTimeByCarId(carId);
				if(time!=null&&!time.equals("")) {
					ma.put("time", time);
				}else {
					ma.put("time", "");
				}
				JfzCarType ct=jfzCarTypeMapper.selectByCarId(carId);
				if(ct!=null) {
					ma.put("type", ct.getType());
				}else {
					ma.put("type", 0);
				}
				for(String po:positionNum) {
					Map<String, Object> positionMap=jfzRealTimeMapper.selectCarByIdAndPosition(carId, po);
					if(positionMap!=null) {
						if(positionMap.get("voltage")!=null) {
							ma.put("voltage"+po, positionMap.get("voltage"));
						}else {
							ma.put("voltage"+po, "0");
						}
						if(positionMap.get("temperature")!=null) {
							ma.put("temperature"+po, positionMap.get("temperature"));
						}else {
							ma.put("temperature"+po, "0");
						}
						if(positionMap.get("tire_pressure")!=null) {
							ma.put("tire_pressure"+po, positionMap.get("tire_pressure"));
						}else {
							ma.put("tire_pressure"+po, "0");
						}
					}else {
						ma.put("voltage"+po, "0");
						ma.put("temperature"+po, "0");
						ma.put("pressure"+po, "0");
					}
				}*/
			int total=jfzRealTimeMapper.getCount();
			map.put("data", data);
			map.put("total", total);
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

    @Override		// aaaaaa
    public Option getEcharts(String startTime, String endTime, int type,int carId) {
        // TODO Auto-generated method stub
        String sql = "";
        if (startTime != null&&!startTime.equals("")) {
            sql += " AND recieve_time>='" + startTime+"'";
        }else {
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sql += " AND recieve_time>='" + DataUtil.getPreTime(new Date(),"-5760")+"'";
        }
        if (endTime != null&&!endTime.equals("")) {
            sql += " AND recieve_time<='" + endTime+"'";
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sql += " AND recieve_time<='" + sdf.format(new Date())+"'";
        }
        Option op=new Option();
        if(type==1) {
            op.setTitle(new Title().text("胎温曲线图(℃)"));
        }else if(type==2) {
            op.setTitle(new Title().text("胎压曲线图(Bar)"));
        }else {
            op.setTitle(new Title().text("速度曲线图(Km/h)"));
        }
        op.tooltip(Trigger.axis);

        op.setGrid(new Grid().left("3%").right("4%").bottom("3%"));
        op.toolbox().show(true).feature(Tool.mark,Tool.saveAsImage,Tool.dataZoom);
        op.yAxis(new ValueAxis().type(AxisType.value));
        //for(String po:CarTypeEnum.CARTYPE7.getPosition()) {
        List<String> time=new ArrayList<>();
        if(type!=3) {
            time=jfzRealTimeMapper.selectRecieveTimeByCarId(carId, sql);
            List<Map<String, Object>> list=new ArrayList<>();
            if (endTime != null&&!endTime.equals("")) {
                time.add(endTime);
            }
            ErpCustomerCar car=erpCustomerCarMapper.selectById(carId);
            Integer type1=0;
            if(car!=null) {
                type1=car.getCarType3();
            }
            positionNum= PositonUtil.getByTypeId(type1);
            String[] xin=PositonUtil.getChineseByTypeId(type1);
            int count=0;
            for(String po:positionNum) {
                Map<String, String> map=new HashMap<>();
                List<String> data=new ArrayList<>();
                if(type==1) {
                    list=jfzRealTimeMapper.selectTemperatureByCarIdAndPosition(po, carId, sql);
                }else if(type==2) {
                    list=jfzRealTimeMapper.selectVoltageByCarIdAndPosition(po, carId, sql);
                }
                for(Map<String, Object> ma:list) {
                    if(type==1) {
                        map.put(ma.get("recieve_time").toString(), ma.get("temperature").toString());
                    }else if(type==2) {
                        Double tire_pressure=Double.valueOf(ma.get("tire_pressure").toString());
                        tire_pressure=tire_pressure/100;
                        map.put(ma.get("recieve_time").toString(), tire_pressure+"");
                    }
                }
                String num="";
                for(String ti:time) {
                    if(map.isEmpty()||map.size()==0) {
                        data.add("0");
                    }else {
                        if(map.get(ti)==null) {
                            if(map.get(num)==null) {
                                data.add("0");
                            }else {
                                data.add(map.get(num));
                            }
                        }else {
                            data.add(map.get(ti));
                            num=ti;
                        }

                    }
                }
                Line line=new Line();
                line.setData(data);
                line.name(xin[count]);
                line.smooth(true);
                op.series(line);
                count++;
            }
            op.setLegend(new Legend(xin));
        }else {
            op.setLegend(new Legend("车速"));
            List<Map<String, Object>> list=jfzRealTimeMapper.selectSpeedByCarIdAndPosition( carId, sql);
            List<String> data=new ArrayList<>();
            for(Map<String, Object> ma:list) {
                time.add(ma.get("recieve_time").toString());
                if(ma.get("car_speed")!=null) {
                    data.add(ma.get("car_speed").toString());
                }else {
                    data.add("0");
                }
            }
            Line line=new Line();
            line.setData(data);
            line.name("车速");
            line.smooth(true);
            op.series(line);
        }

        CategoryAxis categoryAxis = new CategoryAxis();
        categoryAxis.type(AxisType.category);
        categoryAxis.setData(time);
        op.xAxis(categoryAxis);
        //}

        return op;
    }


	@Override
	public Option getEcharBing(String startTime, String endTime,int type) {
		List<Map<String,Object>> map =new ArrayList<>();
		String [] searchs={};
		Option op=new Option();
		if(type==1) {
			op.setTitle(new Title().text("报警原因饼图"));
			map=jfzRealTimeMapper.selectReason(startTime,endTime);

        }else if(type==2) {
			op.setTitle(new Title().text("报警类型饼图"));
			map=jfzRealTimeMapper.selectType(startTime,endTime);
		}else {
			op.setTitle(new Title().text("胎位饼图"));
			map=jfzRealTimeMapper.selectLocation(startTime,endTime);
		}
        List<Integer> count=new ArrayList<>();
        List<String> Type=new ArrayList<>();
        for (int i = 0; i < map.size(); i++) {
            Map<String, Object> stringObjectMap =  map.get(i);
            Type.add(String.valueOf(stringObjectMap.get("name")));
            count.add(Integer.valueOf(stringObjectMap.get("value").toString()));
        }
        int[] d = new int[count.size()];
        for(int i = 0;i<count.size();i++){
            d[i] = count.get(i);
        }

        searchs=Type.toArray(new String[Type.size()]);
		op.tooltip(Trigger.item);
		op.tooltip().formatter("{a} <br/>{b} : {c} ({d}%)");
		op.legend().data(searchs).type(scroll).orient(Orient.vertical).right(10).top(20).bottom(20);
		//.selected()缺个这个
		//for(String po:CarTypeEnum.CARTYPE7.getPosition()) {
        Pie pie = new Pie();//创建饼图对象
        pie.name("数据").radius("55%").center("40%","50%").itemStyle().emphasis().shadowBlur(10).shadowOffsetX(0).shadowColor("'rgba(0, 0, 0, 0.5)'");
        //填充数据
        for(int i = 0; i < searchs.length; i++){
            Map<String,Object> map1 = new HashMap<>();
            map1.put("value",d[i]);//填充饼图数据
            map1.put("name",searchs[i]);//填充饼图数据对应的搜索引擎
            pie.data(map1);
        }
        pie.data(map);
        op.series(pie);

		return op;
	}
	@Override
	public Option getEcharZhe(String startTime, String endTime,int type) {
		List<Map<String,Object>> map =new ArrayList<>();
		String [] searchs={};
		Option op=new Option();
		if(type==1) {
			op.setTitle(new Title().text("报警原因折线图"));
			map=jfzRealTimeMapper.selectReason(startTime,endTime);

        }else if(type==2) {
			op.setTitle(new Title().text("报警类折线图"));
			map=jfzRealTimeMapper.selectType(startTime,endTime);
		}else {
			op.setTitle(new Title().text("胎位折线图"));
			map=jfzRealTimeMapper.selectLocation(startTime,endTime);
		}
        List<Integer> count=new ArrayList<>();
        List<String> Type=new ArrayList<>();
        for (int i = 0; i < map.size(); i++) {
            Map<String, Object> stringObjectMap =  map.get(i);
            Type.add(String.valueOf(stringObjectMap.get("name")));
            count.add(Integer.valueOf(stringObjectMap.get("value").toString()));
        }
        int[] d = new int[count.size()];
        for(int i = 0;i<count.size();i++){
            d[i] = count.get(i);
        }
        op.yAxis(new ValueAxis().type(AxisType.value));
        searchs=Type.toArray(new String[Type.size()]);
        CategoryAxis categoryAxis = new CategoryAxis();
        categoryAxis.type(AxisType.category);
        categoryAxis.setData(Type);
        op.xAxis(categoryAxis);
        Line line=new Line();
        line.setData(count);
        line.smooth(true);
        op.series(line);
		return op;
	}

	@Override
	public Option getEcharZhu(String startTime, String endTime,int type) {
		// TODO Auto-generated method stub
		List<Map<String,Object>> map =new ArrayList<>();
		String [] searchs={};
		Option op=new Option();
		if(type==1) {
			op.setTitle(new Title().text("报警原因柱状图"));
			map=jfzRealTimeMapper.selectReason(startTime,endTime);

        }else if(type==2) {
			op.setTitle(new Title().text("报警类柱状图"));
			map=jfzRealTimeMapper.selectType(startTime,endTime);
		}else {
			op.setTitle(new Title().text("胎位柱状图"));
			map=jfzRealTimeMapper.selectLocation(startTime,endTime);
		}
        List<Integer> count=new ArrayList<>();
        List<String> Type=new ArrayList<>();
        for (int i = 0; i < map.size(); i++) {
            Map<String, Object> stringObjectMap =  map.get(i);
            Type.add(String.valueOf(stringObjectMap.get("name")));
            count.add(Integer.valueOf(stringObjectMap.get("value").toString()));
        }
        int[] d = new int[count.size()];
        for(int i = 0;i<count.size();i++){
            d[i] = count.get(i);
        }
        op.setGrid(new Grid().left("3%").right("4%").bottom("3%").containLabel(true));
        op.yAxis(new ValueAxis().type(AxisType.value));
        searchs=Type.toArray(new String[Type.size()]);
        CategoryAxis categoryAxis = new CategoryAxis();
        categoryAxis.type(AxisType.category);
//        categoryAxis.axisTick();  //不知道咋填

        categoryAxis.setData(Type) ;
        op.xAxis(categoryAxis);
        Bar bar=new Bar();
        bar.name("数据").barWidth(60);
        bar.setData(count);
        op.series(bar);
        op.color("'#3398DB'");
		return op;
	}

	@Override
	public Map<String, Object> getListByErrorCar() {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> map=new HashMap<>();
			List<Map<String, Object>> data=new ArrayList<>();
			List<Map<String, Object>> list=jfzRealTimeMapper.selectStateErrorCar();
			for(Map<String, Object> ma:list) {
				Map<String, Object> da=jfzRealTimeMapper.getErrorCarList(ma.get("tire_location").toString(),Integer.parseInt(ma.get("car_id").toString()));
				data.add(da);
			}
			
				//List<Map<String, Object>> data=new ArrayList<>();
				/*for(Map<String, Object> ma:data) {
					int carId=Integer.parseInt(ma.get("id").toString());
					Double speed=jfzRealTimeMapper.selectSpeedByCarId(carId);
					if(speed==null) {
						ma.put("speed", 0);
					}else {
						ma.put("speed", speed);
					}
					String time=jfzRealTimeMapper.selectTimeByCarId(carId);
					if(time!=null&&!time.equals("")) {
						ma.put("time", time);
					}else {
						ma.put("time", "");
					}
					JfzCarType ct=jfzCarTypeMapper.selectByCarId(carId);
					if(ct!=null) {
						ma.put("type", ct.getType());
					}else {
						ma.put("type", 0);
					}
					for(String po:positionNum) {
						Map<String, Object> positionMap=jfzRealTimeMapper.selectCarByIdAndPosition(carId, po);
						if(positionMap!=null) {
							if(positionMap.get("voltage")!=null) {
								ma.put("voltage"+po, positionMap.get("voltage"));
							}else {
								ma.put("voltage"+po, "0");
							}
							if(positionMap.get("temperature")!=null) {
								ma.put("temperature"+po, positionMap.get("temperature"));
							}else {
								ma.put("temperature"+po, "0");
							}
							if(positionMap.get("tire_pressure")!=null) {
								ma.put("tire_pressure"+po, positionMap.get("tire_pressure"));
							}else {
								ma.put("tire_pressure"+po, "0");
							}
						}else {
							ma.put("voltage"+po, "0");
							ma.put("temperature"+po, "0");
							ma.put("pressure"+po, "0");
						}
					}
					
				}*/
				map.put("data", data);
				return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Map<String, Object> selectTireArchives(String po, int carId) {
		// TODO Auto-generated method stub
		ErpCarTireArchives tire=jfzRealTimeMapper.selectTireArchives(carId, po);
		Map<String, Object> map=new HashMap<>();
		if(tire!=null) {
			ErpMateriel materiel=erpMaterielMapper.selectById(tire.getTireId());
			if(materiel!=null) {
				map.put("materiel", materiel);
				ErpMateriel sonMateriel=erpMaterielMapper.selectById(tire.getSensorsId());
				if(sonMateriel!=null) {
					map.put("sonMateriel", sonMateriel.getMaterielProductionId());
				}else {
					map.put("sonMateriel", "");
				}
				map.put("li", tire.getTirePurchaseMileage());
				ErpTireArchives archives=erpTireArchivesMapper.selectByTireId(tire.getTireId());
				if(archives!=null) {
					map.put("wen", archives.getTireTread1());
				}else {
					map.put("wen", "");
				}
				return map;
			}else {
				return null;
			}
		}else {
			return null;
		}
	}

	@Override
	public Map<String, Object> selectCarTyre(int carId) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> ma=new HashMap<>();
			ErpCustomerCar car=erpCustomerCarMapper.selectById(carId);
			Integer type=0;
			if(car!=null) {
				type=car.getCarType3();
				ma.put("type", car.getCarType3());
			}else {
				ma.put("type", 0);
			}
			positionNum=PositonUtil.getByTypeId(type);
			if(positionNum!=null) {
				for(String po:positionNum) {
					Map<String, Object> positionMap=jfzRealTimeMapper.selectCarByIdAndPosition(carId, po);
					if(positionMap!=null) {
						/*if(positionMap.get("sensors_id")!=null) {
							ma.put("sensors_id"+po, positionMap.get("sensors_id"));
						}else {
							ma.put("sensors_id"+po, "0");
						}
						if(positionMap.get("positioner_id")!=null) {
							ma.put("positioner_id"+po, positionMap.get("positioner_id"));
						}else {
							ma.put("positioner_id"+po, "0");
						}*/
						if(positionMap.get("voltage")!=null) {
							ma.put("voltage"+po, positionMap.get("voltage"));
						}else {
							ma.put("voltage"+po, "0");
						}
						if(positionMap.get("temperature")!=null) {
							ma.put("sensors_id"+po, positionMap.get("temperature"));
						}else {
							ma.put("sensors_id"+po, "0");
						}
						if(positionMap.get("tire_pressure")!=null) {
							//object 转string
							//然后string 转double

							ma.put("positioner_id"+po,Double.parseDouble(String.valueOf(positionMap.get("tire_pressure")))/100 );
						}else {
							ma.put("positioner_id"+po, "0");
						}
						
					}else {
						ma.put("sensors_id"+po, "0");
						ma.put("positioner_id"+po, "0");
						ma.put("voltage"+po, "0");
					}
					Integer state=erpTirePoliceMapper.selectPoliceType(carId, po);
					Integer states=0;
					if(positionMap==null||positionMap.get("state")==null) {
						
					}else {
						states=Integer.parseInt(positionMap.get("state").toString());
					}
					ma.put("police_type"+po, "0");
					if(state==null&&states==1) {
						ma.put("police_type"+po, "0");
					}else {
						if(state==null&&states==0) {
							ma.put("police_type"+po, "5");             //未连接状态 以后需要修改
						}else if(state!=null){
							ma.put("police_type"+po, state);
						}
					}
				}
				return ma;
			}else {
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Result<List<Map<String, Object>>> selectByCustomeId(Integer cId) {
		// TODO Auto-generated method stub
		ErpCustomer cust=erpCustomerMapper.selectById(cId);
		List<ErpCustomer> list=erpCustomerMapper.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com", cust.getCusCode()));
		List<Integer> custIdList=new ArrayList<>();
		custIdList.add(cId);
		for(ErpCustomer customer:list) {
			custIdList.add(customer.getId());
		}
		String custIds=BlankUtil.listToString(custIdList);
		List<Map<String, Object>> carList=jfzRealTimeMapper.selectByCustomer(custIds);
		return new Result<List<Map<String,Object>>>(carList,carList.size());
	}

	@Override
	public Result<List<Map<String, Object>>> selectTable(String startTime,String endTime,Integer cId, Integer type,Integer page,Integer limit) {
		// TODO Auto-generated method stub
		String sql = "";
		if (startTime != null&&!startTime.equals("")) {
			sql += " AND p.police_dt>='" + startTime+"'";
		}else {
			sql += " AND p.police_dt>='" + DataUtil.getMonFirst()+" 00:00:00'";
		}
		if (endTime != null&&!endTime.equals("")) {
			sql += " AND p.police_dt<='" + endTime+"'";
		}
		//List<Map<String, Object>> list=new ArrayList<>();
		List<Map<String, Object>> carList=jfzRealTimeMapper.selectTableByCid(cId,page,limit);
		int count=jfzRealTimeMapper.selectTableByCidCount(cId);
		int zong=0;
		for(Map<String, Object> map:carList) {
			//int id=Integer.parseInt(map.get("id").toString());
			//ErpTpmsTireInformation tpms=erpTpmsTireInformationMapper.selectByCId(id);
			int carId=Integer.parseInt(map.get("carId").toString());
			List<String> timeList=jfzRealTimeMapper.selectTimeScope(carId, sql);
			if(timeList!=null&&timeList.size()>0) {
				String startTime1=timeList.get(0);
				String endTime1=timeList.get(timeList.size()-1);
				Map<String, Object> ma=jfzRealTimeMapper.normalStatistics(startTime1, endTime1, carId);
				map.put("zheng_num", ma.get("num"));
				map.put("zheng_time", ma.get("time"));
			}else {
				map.put("zheng_num", "");
				map.put("zheng_time", "");
			}
			zong+=timeList.size();
			List<ErpTirePolice> policeList=jfzRealTimeMapper.selectPoliceList(carId, 0.1, 0.15);
			if(policeList!=null&&policeList.size()>0) {
				String startTime1=policeList.get(0).getPoliceDt();
				String endTime1=policeList.get(policeList.size()-1).getPoliceDt();
				Map<String, Object> ma=jfzRealTimeMapper.normalStatistics(startTime1, endTime1, carId);
				map.put("ten_num", ma.get("num"));
				map.put("ten_time", ma.get("time"));
			}else {
				map.put("ten_num", "");
				map.put("ten_time", "");
			}
			zong+=policeList.size();
			List<ErpTirePolice> policeList1=jfzRealTimeMapper.selectPoliceList1(carId, 0.15);
			if(policeList1!=null&&policeList1.size()>0) {
				String startTime1=policeList1.get(0).getPoliceDt();
				String endTime1=policeList1.get(policeList1.size()-1).getPoliceDt();
				Map<String, Object> ma=jfzRealTimeMapper.normalStatistics(startTime1, endTime1, carId);
				map.put("twenty_num", ma.get("num"));
				map.put("twenty_time", ma.get("time"));
			}else {
				map.put("twenty_num", "");
				map.put("twenty_time", "");
			}
			zong+=policeList1.size();
			map.put("zheng_bi", BlankUtil.toOneHundred(zong, timeList.size()));
			map.put("ten_bi", BlankUtil.toOneHundred(zong, policeList.size()));
			map.put("twenty_bi",  BlankUtil.toOneHundred(zong, policeList1.size()));
		}
		return new Result<List<Map<String,Object>>>(carList,count);
	}

	@Override
	public Result<List<Map<String, Object>>> selectErrorList(String startTime,String endTime,Integer cId, Integer page, Integer limit) {
		// TODO Auto-generated method stub
		try {
			String sql = "";
			if (startTime != null&&!startTime.equals("")) {
				sql += " AND p.police_dt>='" + startTime+"'";
			}else {
				sql += " AND p.police_dt>='" + DataUtil.getMonFirst()+" 00:00:00'";
			}
			if (endTime != null&&!endTime.equals("")) {
				sql += " AND p.police_dt<='" + endTime+"'";
			}
			List<Map<String, Object>> list=jfzRealTimeMapper.selectErrorListByCid(cId, sql, (page-1)*limit, limit);
			int count=jfzRealTimeMapper.selectErrorListByCidCount(cId, sql);
			return new Result<List<Map<String,Object>>>(list, count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

//    @Override
//    public List<Map<String, Object>> selectReason(String startTime, String endTime) {
//        return null;
//    }
//
//    @Override
//    public List<Map<String, Object>> selectType(String startTime, String endTime) {
//        return null;
//    }
//
//    @Override
//    public List<Map<String, Object>> selectLocation(String startTime, String endTime) {
//        return null;
//    }

//	@Override
//	public List<Map<String, Object>> selectReason(String startTime, String endTime) {
//		return jfzRealTimeMapper.selectReason(startTime,endTime);
//	}
//
//	@Override
//	public List<Map<String, Object>> selectType(String startTime, String endTime) {
//		return jfzRealTimeMapper.selectType(startTime,endTime);
//	}
//
//	@Override
//	public List<Map<String, Object>> selectLocation(String startTime, String endTime) {
//		return jfzRealTimeMapper.selectLocation(startTime,endTime);
//	}

}
